package com.atguigu.crowd.handler;

import com.atguigu.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@RestController
public class RedisHandler {

    @Autowired
    private StringRedisTemplate redisTemplate;

    //设置key value
    @RequestMapping("/set/redis/key/value/remote")
    ResultEntity<String> setRedisKeyValueRemote(
            @RequestParam("key") String key,
            @RequestParam("value") String value) {

        try {
            ValueOperations<String, String> operations = redisTemplate.opsForValue();

            operations.set(key, value);

            return ResultEntity.successWithoutData();
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }

    }

    //带超时事件的key value
    @RequestMapping("/set/redis/key/value/remote/with/timeout")
    ResultEntity<String> setRedisKeyValueRemoteWithTimeout(
            @RequestParam("key") String key,
            @RequestParam("value") String value,
            @RequestParam("time") long time,
            @RequestParam("timeUnit") TimeUnit timeUnit) {
            //TimeUnit 时间单位
        try {
            ValueOperations<String, String> operations = redisTemplate.opsForValue();

            operations.set(key, value, time, timeUnit);

            return ResultEntity.successWithoutData();
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }
    }

    //获取
    @RequestMapping("/get/redis/string/value/by/key")
    ResultEntity<String> getRedisStringValueByKeyRemote(@RequestParam("key") String key) {

        try {
            ValueOperations<String, String> operations = redisTemplate.opsForValue();

            String value = operations.get(key);

            return ResultEntity.successWithData(value);
        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }

    }

    @RequestMapping("/remove/redis/key/remote")
    ResultEntity<String> removeRedisKeyRemote(@RequestParam("key") String key) {
        try {

            redisTemplate.delete(key);

            return ResultEntity.successWithoutData();

        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }
    }


    //ZSET操作
    // ZINCRBY key increment member
    //有序集合中对指定成员的分数加上增量 increment
    @RequestMapping("zset/redis/key/score/value/remote")
    ResultEntity<String> setRedisZsetRemote(
            @RequestParam("key") String key,
            @RequestParam("score")double score,
            @RequestParam("projectName") String projectName
            ) {
        try {
            redisTemplate.opsForZSet().incrementScore(key, projectName, score);

            return ResultEntity.successWithoutData();

        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }
    }


    //redis 排行榜 zset range
    @RequestMapping("zrevrange/redis/key/score/value/remote")
    ResultEntity<String> getRedisRangeRemote(
            @RequestParam("key") String key,
            @RequestParam("startIndex")Integer startIndex,
            @RequestParam("endIndex")Integer endIndex
    ) {
        try {
            Set<String> reverseRange = redisTemplate.opsForZSet().reverseRange(key, startIndex, endIndex);

//            System.out.printf("排行榜：%s\n",reverseRange.toString());
            return ResultEntity.successWithData(reverseRange.toString());

        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }
    }

    //


    //设置zset的score 排行榜 每隔一段时间设置为0
    @RequestMapping("zsetAll/redis/key/score/value/remote")
    ResultEntity<String> setRedisZsetAllRemote(
            @RequestParam("key") String key,
            @RequestParam("score")Integer score
    ){
        try {

            Set<String> range = redisTemplate.opsForZSet().range(key, 0, -1);
            System.out.println(range);
            //遍历集合 设置值
            for (String i:range) {
                redisTemplate.opsForZSet().add(key,i, score);
            }
            return ResultEntity.successWithoutData();

        } catch (Exception e) {
            e.printStackTrace();

            return ResultEntity.failed(e.getMessage());
        }

    }


}
